package io.socket.my;

import com.google.gson.Gson;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) throws URISyntaxException, InterruptedException {
        IO.Options options = new IO.Options();
        options.transports = new String[]{"websocket"};
        options.reconnectionAttempts = 2;
        //失败重连的时间间隔
        options.reconnectionDelay = 1000;
        //连接超时时间(ms)
        options.timeout = 1000;
        options.path = "/ws";

        final Socket socket = IO.socket("http://localhost:9099", options);

        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println(DataUtil.getCurrentDataTime() + ":client connect! ");

                socket.send("hello server, my name is client");
            }
        });

        socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println(DataUtil.getCurrentDataTime() + ":client disconnect!");
            }
        });

        socket.on("chatevent", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println(DataUtil.getCurrentDataTime() + ":chatevent");
            }
        });

        socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                for (Object obj : args) {
                    System.out.println(DataUtil.getCurrentDataTime() + ":receive server message=" + obj);
                }
            }
        });

        socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println("connect error");
            }
        });

        socket.connect();

        System.out.println(DataUtil.getCurrentDataTime() + ":client console input......");
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String message = sc.next();
            System.out.println(DataUtil.getCurrentDataTime() + ":client console send data=" + message);

            if (socket.connected()) {
                ChatObject chat = new ChatObject("chris", "helloworld");
                String json = new Gson().toJson(chat);

                // TODO: 2019-03-07 此方式无法通信？
                socket.emit("chatevent", json);
                // socket.send(message);
            }
        }

    }
}

class ChatObject implements Serializable {

    private String userName;
    private String message;

    public ChatObject() {
    }

    public ChatObject(String userName, String message) {
        this.userName = userName;
        this.message = message;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}